﻿//------------------------------------------------------------------------------
// ╭─────────────────────────────╮
// │ ╭─╮     ╭─╮              TM │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// │ │ │     │ │                 │                                           ☺  
// │ │ ╰───╮ │ │ ╭─────╮ ╭─────╮ │     Name:Wedn.Net 服务端Session操作助手类  ♮ ♪ ♩
// │ │ ╭─╮ │ │ │ │ ╭─╮ │ │ ╭─╮ │ │                                     ♫ ♭      
// │ │ ╰─╯ │ │ │ │ ╰─╯ │ │ ╰─╯ │ │     Author:iceStone                ♬ ♪       
// │ └─────╯ └─╯ ╰─────╯ ╰───╮ │ │     Chinese:汪磊                              
// │                     ┌───╯ │ │                                              
// │                     ╰─────╯ │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// ╰─────────────────────────────╯                                              
//------------------------------------------------------------------------------
using System;
using System.Web;
using System.Web.Caching;
using Wedn.Net.Common;

namespace Wedn.Net.Utility
{
    /// <summary>
    /// 服务端Session操作助手类
    /// </summary>
    public static class SessionHelper
    {
        #region 向Session中添加字符串，调动有效期为30分钟 +static void AddSession(string name, string value)
        /// <summary>
        /// 向Session中添加字符串，调动有效期为30分钟
        /// </summary>
        /// <param name="name">Session对象名称</param>
        /// <param name="value">Session对象</param>
        public static void AddSession(string name, string value)
        {
            SetSession(name, value, 30);
        } 
        #endregion

        #region 向Session中添加对象，调动有效期为30分钟 +static void AddSession(string name, object value)
        /// <summary>
        /// 向Session中添加对象，调动有效期为30分钟
        /// </summary>
        /// <param name="name">Session对象名称</param>
        /// <param name="value">Session对象</param>
        public static void AddSession(string name, object value)
        {
            SetSession(name, value, 30);
        } 
        #endregion

        #region 向Session中添加对象，调动有效期为30分钟 +static void AddSession(string name, object value, int expires)
        /// <summary>
        /// 向Session中添加对象，调动有效期为30分钟
        /// </summary>
        /// <param name="name">Session对象名称</param>
        /// <param name="value">Session对象</param>
        /// <param name="expires">超时时间</param>
        public static void AddSession(string name, object value, int expires)
        {
            SetSession(name, value, expires);
        } 
        #endregion

        #region 向Session中添加对象，调动有效期为20分钟 +static void SetSession(string name, object value)
        /// <summary>
        /// 向Session中添加对象，调动有效期为20分钟
        /// </summary>
        /// <param name="name">Session对象名称</param>
        /// <param name="value">Session对象</param>
        public static void SetSession(string name, object value)
        {
            SetSession(name, value, 30);
        } 
        #endregion

        #region 向Session中添加对象 +static void SetSession(string name, object value, int expires)
        /// <summary>
        /// 向Session中添加对象
        /// </summary>
        /// <param name="name">Session对象名称</param>
        /// <param name="value">Session对象</param>
        /// <param name="expires">超时时间</param>
        public static void SetSession(string name, object value, int expires)
        {
            //HttpContext.Current.Session[name] = value;
            //HttpContext.Current.Session.Timeout = expires;
            string cookie = CookieHelper.GetCookie("SessionId");
            string sessionId = string.IsNullOrEmpty(cookie) ? Guid.NewGuid().ToString() : cookie;
            CookieHelper.SetCookie("SessionId", sessionId, DateTime.Now.AddMinutes(expires));
            var cache = HttpRuntime.Cache;
            cache.Insert(Config.SitePrefix + name + "_" + sessionId, value, null, DateTime.MaxValue, TimeSpan.FromMinutes(expires), CacheItemPriority.NotRemovable, null);
        } 
        #endregion

        #region 读取某个Session中字符串值 +static string GetSession(string name)
        /// <summary>
        /// 读取某个Session中字符串值
        /// </summary>
        /// <param name="name">Session名称</param>
        /// <returns>Session中字符串值</returns>
        public static string GetSession(string name)
        {
            //var session = HttpContext.Current.Session[name];
            //return session == null ? null : session.ToString();
            var sessionId = CookieHelper.GetCookie("SessionId");
            var cache = HttpRuntime.Cache[Config.SitePrefix + name + "_" + sessionId];
            return cache == null ? string.Empty : cache.ToString();
        } 
        #endregion

        #region 读取某个Session中对象 +static T GetSession<T>(string name) where T : class
        /// <summary>
        /// 读取某个Session中对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="name">Session名称</param>
        /// <returns>Session中对象</returns>
        public static T GetSession<T>(string name) where T : class
        {
            //var session=HttpContext.Current.Session[name];
            //return session == null ? null : session as T;
            var sessionId = CookieHelper.GetCookie("SessionId");
            var cache = HttpRuntime.Cache[Config.SitePrefix + name + "_" + sessionId];
            return cache == null ? null : cache as T;
        } 
        #endregion

        #region 删除某个Session对象 +static void RemoveSession(string name)
        /// <summary>
        /// 删除某个Session对象
        /// </summary>
        /// <param name="name">Session对象名称</param>
        public static void RemoveSession(string name)
        {
            //HttpContext.Current.Session.Remove(name);
            var sessionId = CookieHelper.GetCookie("SessionId");
            var cache = HttpRuntime.Cache;
            cache.Remove(Config.SitePrefix + name + "_" + sessionId);
        } 
        #endregion
    }
}